<template>
    <div class="right-box">
        <div>
            <span class="information-zh">账号信息</span>

            <submit-head-img ref="headImg" style="margin-bottom: 20px"></submit-head-img>

            <div class="information-zh-text">
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

                    <el-form-item label="昵称" prop="nickName" style="width: 300px">
                        <el-input v-model.number="ruleForm.nickName"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                        <el-button @click="resetForm('ruleForm')">恢复</el-button>
                    </el-form-item>
                </el-form>
            </div>

        </div>
    </div>
</template>

<script>
    import {request} from "../../network/request";
    import submitHeadImg from "./submitHeadImg";
    export default {
        name: "userInfomation",
        data() {

            var checkName = (rule, value, callback) => {
                value = value + '';
                if (value === '') {
                    callback(new Error('用户名不能为空'));
                } else if (value.length >= 10) {
                    callback(new Error('用户名的长度不能超过10'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    nickName: sessionStorage.getItem('nickName'),
                },
                rules: {
                    nickName: [
                        { validator: checkName, trigger: 'blur' }
                    ]
                },
                phoneNumber: sessionStorage.getItem('phoneNumber')
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs.headImg.uploading();
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        request({
                            url:"/updateNickName",
                            method: "post",
                            params:{
                                phoneNumber: this.phoneNumber,
                                nickName:this.ruleForm.nickName
                            }
                        }).then(res=>{
                            this.$message({
                                message: '修改成功',
                                type: 'success'
                            });
                            sessionStorage.setItem('nickName', this.ruleForm.nickName);
                            // location.reload();
                        }).catch(err=>{
                            console.log(err);
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
        components:{
            submitHeadImg
        },
        created() {
            //  this.$router.push("/information/myVideo");
        }
    }
</script>

<style scoped>
    .information-zh{
        display: inline-block;
        padding: 10px;
        color: #222226;
        font-size: 16px;
        font-weight: bold;
    }


    .right-box{
        height: 700px;
        width: 800px;
        background-color: #fff;
        position: relative;
        left: 150px;
        top: -278px;
        display: inline-block;
    }
</style>